package com.example.exam001.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;

import java.util.ArrayList;
import java.util.List;

@Configuration
@Slf4j
public class AppConfig {

    @Autowired
    private ApplicationContext applicationContext;

    public List<Class<?>> domainClassList() {

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(applicationContext.getClassLoader());
        List<Class<?>> classes = new ArrayList<>();
        try {
            Resource[] resources = resolver.getResources("classpath*:com/example/exam001/domain/*.class"); // 请将'your/package/path'替换为您的实际包路径
            MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resolver);
            for (Resource r : resources) {
                MetadataReader reader = readerFactory.getMetadataReader(r);
                String className = reader.getClassMetadata().getClassName();
                classes.add(Class.forName(className));
                log.info(className);
            }
        } catch (Exception e) {
            // Handle exception
            e.printStackTrace();
        }
        return classes;
    }
}
